/**
 * Copyright (C) 2010-2013 Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.rocketmq.example.quickstart;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;

/**
 * Producer，发送消息
 * 
 */
public class Producer {
	public static void main(String[] args) throws MQClientException, InterruptedException {
		DefaultMQProducer producer = new DefaultMQProducer("ProducerTEST_GROUP");

		// 设置了环境变量NAMESRV_ADDR 10.244.66.142:9876;10.244.66.142:8765 并且properties中设置fetchNamesrvAddrByAddressServer=true则下面不需要设置setNamesrvAddr
		// producer.setNamesrvAddr("10.224.66.142:9876;10.224.66.142:8765");
		// producer.setNamesrvAddr("192.168.1.102:9876");
		producer.setNamesrvAddr("10.230.20.226:8765");
		// producer.setInstanceName("instanceNameProducer");
		producer.start();

		for (int i = 1; i <= 1; i++) {

			try {
				Message msg = new Message("TopicTest01", // topic
						"TagA", // tag
						String.valueOf(System.currentTimeMillis()), // keys
						("Hello RocketMQ TagA" + i).getBytes()// body
				);
				SendResult sendResult = producer.send(msg);

				System.out.println(sendResult);
			} catch (Exception e) {
				e.printStackTrace();
				Thread.sleep(1000);
			}
		}

		producer.shutdown();

		/*
		 * DefaultMQProducer producer1 = new DefaultMQProducer("ProducerTEST1_GROUP"); producer1.setNamesrvAddr("10.230.20.224:9876"); //producer1.setInstanceName("instanceName2"); producer1.start();
		 * 
		 * for (int i = 0; i < 1; i++) { try { Message msg = new Message("TopicTest", // topic "TagA", // tag ("Hello RocketMQ 2" + i).getBytes()// body ); SendResult sendResult = producer1.send(msg); System.out.println(sendResult); } catch (Exception e) { e.printStackTrace(); Thread.sleep(1000); } }
		 * 
		 * producer1.shutdown();
		 */
	}
}
